package com.example.newmessageboard.Controller;

import com.example.newmessageboard.Service.MessageService;
import com.example.newmessageboard.model.Messageinfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RequestMapping("/1")
@RestController   //这个注解 可以返回数据   @Controller这个注解时返回视图的
public class MessageController {
    @Autowired
    private MessageService messageService;
    @RequestMapping (value = "/2")
    public String publish(@RequestBody Messageinfo messageinfo) {
            if (!StringUtils.hasLength(messageinfo.getFrom())
                    || !StringUtils.hasLength(messageinfo.getTo())
                    || !StringUtils.hasLength(messageinfo.getMessage())) {
            return "{\"OK\":0}";
        }
        messageService.InsertMessage(messageinfo);
        return "{\"OK\":1}";
    }
    @RequestMapping("/3")
    public List<Messageinfo> getlist() {
        return messageService.getMessageinfo();
    }
}

